const express = require('express')
const request = require('request')
const ejs = require('ejs')
const app = express()
const port = 8080

app.set('views', './views');
app.engine('html', ejs.__express);
app.set('view engine', 'html');
app.use(express.static('public'));

app.get('/list', function (req, res) {
    let url = "http://dormitory:8083/dorm/list";
    request({
            url: url,
            method: "GET",
            headers: {
                token: req.headers.token,
            },
        },
        function (error, response, body) {
            console.log(body)
            res.send(body);
        });
});

app.post('/result', function (req, res) {
    let url = "http://dormitory:8083/dorm/result";
    req.on('data', function (data) {
        obj = JSON.parse(data);
        console.log(obj);
        request({
                url: url,
                method: "POST",
                json: true,
                headers: {
                    token: req.headers.token,
                    contentType: 'application/x-www-form-urlencoded',
                },
                form: {studentNo: obj.studentNo}
            },
            function (error, response, body) {
                console.log(body)
                res.send(body);
            });
    });
});

app.get('/result', function (req, res) {
    let url = "http://dormitory:8083/dorm/result";
    request({
            url: url,
            method: "GET",
            headers: {
                token: req.headers.token,
            },
        },
        function (error, response, body) {
            console.log(body)
            res.send(body);
        });
});

app.get('/login', function (request, response) {
    response.render('login');
});

app.post('/login', function (req, res) {
    let url = "http://login:8081/user/login";
    req.on('data', function (data) {
        obj = JSON.parse(data);
        console.log(obj);
        request({
                url: url,
                method: "POST",
                json: true,
                headers: {contentType: 'application/x-www-form-urlencoded',},
                form: {email: obj.email, passWord: obj.passWord}
            },
            function (error, response, body) {
                console.log(body)
                res.send(body);
            });
    });
});

app.get('/register', function (req, resp) {
    resp.render('register');
});

app.post('/register', (req, resp) => {
    let url = "http://register:8082/register";
    req.on('data', function (data) {
        obj = JSON.parse(data);
        console.log(JSON.stringify(obj));
        request({
            url: url,
            method: "POST",
            json: true,
            headers: {"content-type": "application/json;charset=utf-8",},
            body: obj
        }, function (error, response, body) {
            resp.send(body)
        });
    });
});

app.get('/order', function (req, resp) {
    resp.render('order');
});

app.post('/order', (req, resp) => {
    let url = "http://dormitory:8083/dorm/choose";
    req.on('data', function (data) {
        obj = JSON.parse(data);
        console.log(JSON.stringify(obj));
        console.log("111", req.headers.token, "111");
        request({
            url: url,
            method: "POST",
            json: true,
            headers: {
                contentType: "application/json;charset=utf-8",
                token: req.headers.token,
            },
            body: obj
        }, function (error, response, body) {
            console.log(body);
            resp.send(body);
        });
    });
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})